//
//  MKAPIHttpRequestSimulator.m
//  MKAPI
//
//  Created by 苏尚进 on 2022/9/25.
//  Copyright © 2022 moon. All rights reserved.
//

#import "MKAPIHttpRequestSimulator.h"

@interface MKAPIHttpRequestSimulator ()
@property(nonatomic,strong) NSTimer *simulatorUploadTimer;
@property(nonatomic,strong) NSTimer *simulatorDownloadTimer;
@property(nonatomic,strong) NSTimer *simulatorErrorTimer;

@property(nonatomic,strong) NSProgress *uploadProgress;//上传进度
@property(nonatomic,strong) NSProgress *downloadProgress;//下载进度

@property(nonatomic,strong) NSData *responseData;
@property(nonatomic,strong) NSURLResponse *response;
@property(nonatomic,strong) NSURL *responseFile;//响应返回的下载文件路径

@property(nonatomic,assign) NSTimeInterval timerRepeatDuration;
@end

@implementation MKAPIHttpRequestSimulator
@synthesize responseData;
@synthesize response;
@synthesize responseFile;
- (id)init{
    if(self=[super init]){
        self.simulatorUploadDuration = 0.5;
        self.simulatorDownloadDuration = 0.5;
        self.timerRepeatDuration = 1.0/30;
    }
    return self;
}
- (void)doRunTask{
    [super doRunTask];
    [self.simulatorUploadTimer invalidate];
    [self.simulatorDownloadTimer invalidate];
    
    self.progress.totalUnitCount = 0;
    if(self.simulatorUploadDuration>0){
        self.uploadProgress = [NSProgress progressWithTotalUnitCount:ceil(self.simulatorUploadDuration/self.timerRepeatDuration)];
        self.progress.totalUnitCount += self.uploadProgress.totalUnitCount;
        [self.progress addChild:self.uploadProgress withPendingUnitCount:self.uploadProgress.totalUnitCount];
    }
    if(self.simulatorDownloadDuration>0){
        self.downloadProgress = [NSProgress progressWithTotalUnitCount:ceil(self.simulatorDownloadDuration/self.timerRepeatDuration)];
        self.progress.totalUnitCount += self.downloadProgress.totalUnitCount;
        [self.progress addChild:self.downloadProgress withPendingUnitCount:self.downloadProgress.totalUnitCount];
    }
    [self _beginSimulatorError];
    [self _beginSimulatorUpload];
}
- (void)_beginSimulatorError{
    if(self.simulatorErrorDuration>0&&self.simulatorError){
        [self.simulatorErrorTimer invalidate];
        self.simulatorErrorTimer = [NSTimer scheduledTimerWithTimeInterval:self.simulatorErrorDuration target:self selector:@selector(__onSimulatorErrorTimer:) userInfo:nil repeats:NO];
    }
}
- (void)__onSimulatorErrorTimer:(NSTimer *)timer{
    [self.simulatorErrorTimer invalidate];
    [self.simulatorUploadTimer invalidate];
    [self.simulatorDownloadTimer invalidate];
    [self completeWithError:self.simulatorError];
}
- (void)_beginSimulatorUpload{
    if(self.simulatorUploadDuration>0){
        self.simulatorUploadTimer = [NSTimer scheduledTimerWithTimeInterval:self.timerRepeatDuration target:self selector:@selector(__onSimulatorUploadTimer:) userInfo:nil repeats:YES];
    }else{
        [self _beginSimulatorDownload];
    }
}
- (void)__onSimulatorUploadTimer:(NSTimer *)timer{
    NSUInteger completedUnitCount = self.uploadProgress.completedUnitCount;
    completedUnitCount++;
    completedUnitCount = MIN(completedUnitCount,self.uploadProgress.totalUnitCount);
    self.uploadProgress.completedUnitCount = completedUnitCount;
    [self updateProgress:^(NSProgress * _Nonnull progress) {
    }];
    if(completedUnitCount>=self.uploadProgress.totalUnitCount){
        [self.simulatorUploadTimer invalidate];
        [self _beginSimulatorDownload];
    }
}
- (void)_beginSimulatorDownload{
    if(self.simulatorDownloadDuration>0){
        self.simulatorDownloadTimer = [NSTimer scheduledTimerWithTimeInterval:self.timerRepeatDuration target:self selector:@selector(__onSimulatorDownloadTimer:) userInfo:nil repeats:YES];
    }else{
        [self __didSimulatorSuccess];
    }
}
- (void)__onSimulatorDownloadTimer:(NSTimer *)timer{
    NSUInteger completedUnitCount = self.downloadProgress.completedUnitCount;
    completedUnitCount++;
    completedUnitCount = MIN(completedUnitCount,self.downloadProgress.totalUnitCount);
    self.downloadProgress.completedUnitCount = completedUnitCount;
    [self updateProgress:^(NSProgress * _Nonnull progress) {
    }];
    if(completedUnitCount>=self.downloadProgress.totalUnitCount){
        [self.simulatorDownloadTimer invalidate];
        [self __didSimulatorSuccess];
    }
}
- (void)__didSimulatorSuccess{
    self.response = self.simulatorResponse;
    self.responseData = self.simulatorResponseData;
    self.responseFile = self.simulatorResponseFile;
    [self completeWithError:nil];
}
- (void)dealloc{
    [self.simulatorErrorTimer invalidate];
    [self.simulatorUploadTimer invalidate];
    [self.simulatorDownloadTimer invalidate];
}
- (void)doCancel{
    [super doCancel];
    [self.simulatorErrorTimer invalidate];
    [self.simulatorUploadTimer invalidate];
    [self.simulatorDownloadTimer invalidate];
}
@end
